package com.huidz.calendar.activityadapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huidz.calendar.R;
import com.huidz.calendar.model.BWLModel;
import com.huidz.calendar.util.LunarCalendar;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class BeiWangLuAdapter extends BaseAdapter {
    ArrayList<BWLModel.data> list;
    Context context;

    public BeiWangLuAdapter(ArrayList<BWLModel.data> list, Context context) {
        // TODO Auto-generated constructor stub
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list != null ? list.get(position) : 0;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        convertView = LayoutInflater.from(context).inflate(R.layout.bwllist, null);
        init i = new init();
        i.all = convertView.findViewById(R.id.all);
        i.bwl_list_msg = convertView.findViewById(R.id.bwl_list_msg);
        i.bwl_list_msg_time = convertView.findViewById(R.id.bwl_list_msg_time);
        i.bwl_list_time = convertView.findViewById(R.id.bwl_list_time);
//        if (position % 5 == 0) {
        if (!list.get(position).getType().equals("1"))
            i.all.setVisibility(View.GONE);
//        }
        else {
            i.bwl_list_msg.setText(list.get(position).getMemcontent());
            long sjc = Long.valueOf(list.get(position).getRemindtime());
            String time = getDateToString(sjc);
            i.bwl_list_msg_time.setText(time + " " + s + " " + getWeek(sjc));
        }
        convertView.setTag(i);
        return convertView;
    }

    public static class init {
        //首页list的子控件
        TextView bwl_list_msg, bwl_list_msg_time, bwl_list_time;
        RelativeLayout all;
    }

    public static String s;

    public static String getDateToString(long milSecond) {
        Date date = new Date(milSecond * 1000);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        int y = Integer.parseInt(format.format(date).substring(0, 4));
        int m = Integer.parseInt(format.format(date).substring(5, 7));
        int r = Integer.parseInt(format.format(date).substring(8, 10));
        s = LunarCalendar.getLunarString(y, m, r);
        return format.format(date);
    }

    public static String getWeek(long time) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(new Date(time * 1000));
        int year = cd.get(Calendar.YEAR); //获取年份
        int month = cd.get(Calendar.MONTH); //获取月份
        int day = cd.get(Calendar.DAY_OF_MONTH); //获取日期
        int week = cd.get(Calendar.DAY_OF_WEEK); //获取星期
        String weekString;
        switch (week) {
            case Calendar.SUNDAY:
                weekString = "周日";
                break;
            case Calendar.MONDAY:
                weekString = "周一";
                break;
            case Calendar.TUESDAY:
                weekString = "周二";
                break;
            case Calendar.WEDNESDAY:
                weekString = "周三";
                break;
            case Calendar.THURSDAY:
                weekString = "周四";
                break;
            case Calendar.FRIDAY:
                weekString = "周五";
                break;
            default:
                weekString = "周六";
                break;

        }
        return weekString;
    }
}

